<template>
  <div class="content">
    <div class="management">
      <div v-show="!hasActivities" class="tips">暂无活动，管理员可发布活动</div>
      <Table v-show="hasActivities" :activities="allAcitvities" :loading = "loading"
             :isManager="isManager" @showModal="showModal"></Table>
      <button @click="showModal" v-show="!isManager">发起活动</button>
      <PublicForm v-show="isManager" class="applycation-form" @addActivity="addActivity" ref="form"></PublicForm>
    </div>
  </div>
</template>
<script>
  import Table from './control/Table.vue';
  import PublicForm from './control/PublicForm.vue'
  import axios from 'axios';

  export default {
    name: 'Management',
    data() {
      return {
        hasActivities: true, //默认没有活动
        allAcitvities:[],  //所以的活动
        loading: true, //请求所有之前活动
        isManager: false
      }
    },
    components: {
      Table,
      PublicForm
    },
    methods: {
      //显示输入密码模态框
      showModal() {
        this.$prompt('请管理员密码', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputType:'password'
        }).then(({ value }) => {
          if(value) {
            this.judgePublic(value);
          }
        }).catch(() => {
        });
      },
      //验证是否是管理员
      judgePublic(value){
        let url = 'http://127.0.0.1:1111/api/secure/identify';
        let obj = {
          passWord: value
        };
        let me = this;
        axios({
          method: 'post',
          url: url,
          data: obj,
          withCredentials:true
        }).then(function (response) {
          if(response.data.succeed) {
            me.$message({
              type: 'success',
              message: '密码正确'
            });
            me.isManager = true;
          } else {
            me.$message({
              type: 'info',
              message: '密码错误'
            });
          }
        }).catch(function(err){
          me.$message({
            type: 'info',
            message: '密码错误'
          });
        });
      },

      addActivity(activity){
        activity && this.allAcitvities.push(activity);
      }

    },
    beforeCreate() {
      //查询所以活动
      let me = this;
      let url = 'http://127.0.0.1:1111/api/activities/get/all';
      axios.get(url).then(function (result) {
        var datas = result && result.data.activities;
        if(datas && datas.length > 0) {
          me.hasActivities = true;
          me.allAcitvities = datas;
        }
        me.loading = false;
      }).catch(function(err){
        me.loading = false;
      });
    }
  }

</script>
<style>
  .management {
    background-image: url("./../assets/noActivity.png");
    font-size: 14px;
    min-height: calc(100% - 61px);
    margin-top: 61px;
    position: absolute;
    width: 100%;
    background-size: 100% 100%;
    background-repeat: no-repeat;
  }
  .management .tips {
    font-weight: bold;
    font-size: 28px;
    color: #8EC63F;
    letter-spacing: 1.75px;
    margin-top: 273px;

  }
  .management button {
    width: 280px;
    height: 44px;
    background: #F5A623;
    border-radius: 2px;
    outline: none;
    border: 0;
    margin-top: 32px;
    color: #ffffff;
    letter-spacing: 1.75px;
    font-weight: bold;
    cursor: pointer;
  }
  .management .applycation-form {
    margin-top: 26px;
  }
</style>
